package com.javatimes.test.util;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.javatimes.test.po.Dept;

public abstract class BaseDao<T, PK extends Serializable> extends HibernateDaoSupport{
	
	@SuppressWarnings("unchecked")
	private static Class entityClass;
	private static final Logger log = LoggerFactory.getLogger(Dept.class);//声明控制台输出日志


	@SuppressWarnings({ "unchecked", "static-access" })
	public BaseDao(Class clazz){
		this.entityClass = clazz;
	}
	
	//采用注解方式完成要使用的HibernateDaoSupport的注入，不用在spring配制文件当中注入
	@Autowired
	public void setSessionFactorySub(SessionFactory sessionFactory){
		super.setSessionFactory(sessionFactory);
	}
	
	/**
	 * 保存方法
	 * @param c 为类添加范式
	 */
	public void save(T c){
		log.debug("保存实体类"+entityClass.getSimpleName()+"开始!!!");
		try {
			getSession().save(c);
			log.debug("保存"+entityClass.getSimpleName()+"成功!!!");
		} catch (RuntimeException re) {
			log.error("保存"+entityClass.getSimpleName()+"失败", re);
			throw re;
		}
	}
	
	/**
	 * 删除方法
	 * @param c 为类添加范式
	 */
	public void delete(T c){
		log.debug("删除"+entityClass.getSimpleName()+"开始");
		try {
			getSession().delete(c);
			log.debug("删除"+entityClass.getSimpleName()+"成功");
		} catch (RuntimeException re) {
			log.error("删除"+entityClass.getSimpleName()+"失败", re);
			throw re;
		}
	}
	
	/**
	 * 查找一条数据
	 * @param c 为类添加范式
	 */
	@SuppressWarnings("unchecked")
	public T findById(final PK id){
		log.debug("获取"+entityClass.getSimpleName()+"通过主键: " + id);
		try {
			return  (T) getSession().get(entityClass, id);
		} catch (RuntimeException re) {
			log.error("获取"+entityClass.getSimpleName()+"通过主键: " + id+"失败", re);
			throw re;
		}
	}
   
	/**
	 * 通过属性查询所有数据
	 * @param c 为类添加范式
	 */
	@SuppressWarnings({ "static-access", "unchecked" })
	public List<T> findByProperty(String propertyName, Object value){
		log.debug("查询"+this.entityClass.getSimpleName()+"通过属性: " + propertyName + ", 值: " + value);
		try {
			String queryString = "from "+this.entityClass.getSimpleName()+" as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("通过属性查找失败", re);
			throw re;
		}
	}

	/**
	 * 查找全部数据
	 */
	@SuppressWarnings({ "unchecked", "static-access" })
	public List<T> queryAll(){
		log.debug("查找全部"+this.entityClass.getSimpleName()+"数据!!!");
		try {
			String queryString = "from " + this.entityClass.getSimpleName();
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("查找全部数据失败!!!", re);
			throw re;
		}
	}
	
	@SuppressWarnings("static-access")
	public void update(T d){
		log.debug("修改"+this.entityClass.getSimpleName()+"数据开始");
		try {
			this.getSession().update(d);
			log.debug("修改"+this.entityClass.getSimpleName()+"成功!!!");
		} catch (RuntimeException e) {
			log.error("修改"+this.entityClass.getSimpleName()+"失败!!!",e);
			throw e;
		}
		
	}
	
}
